package me.pplay.playtalk.activity.setting;

import java.util.ArrayList;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.protocol.bean.TpLanguage;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class SettingLanguageActivity extends BaseActivity {
	private ArrayList<TpLanguage> mData = new ArrayList<TpLanguage>();
	private TpChat tpChat;
	private ImageButton btn_back;
	private Button btn_save;
	private RadioGroup radioGroup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_setting_language);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.viewmember_btnback));
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChat = appState.getTpChat();

		initData();
		initRadioGroup();
		btn_back = (ImageButton)findViewById(R.id.lan_btnback);
		btn_save = (Button)findViewById(R.id.lan_btnsave);
		clickEvents();
	}

	private void initData() {
		TpLanguage map = new TpLanguage("ZH", R.string.language_chinese);
		mData.add(map);
		map = new TpLanguage("EN", R.string.language_english);
		mData.add(map);
		//map=new TpLanguage("TR", R.string.language_twchinese);
		map = new TpLanguage("ES", R.string.language_spanish);
		mData.add(map);
		map = new TpLanguage("FR", R.string.language_french);
		mData.add(map);
		map = new TpLanguage("DE", R.string.language_german);
		mData.add(map);
		map = new TpLanguage("IT", R.string.language_italian);
		mData.add(map);
		map = new TpLanguage("TH", R.string.language_thai);
		mData.add(map);
		map = new TpLanguage("RU", R.string.language_russian);
		mData.add(map);
		map = new TpLanguage("UK", R.string.language_ukrainian);
		mData.add(map);
		map = new TpLanguage("PT", R.string.language_portuguese);
		mData.add(map);
		map = new TpLanguage("TR", R.string.language_turkish);
		mData.add(map);
		map = new TpLanguage("VI", R.string.language_vietnamese);
		mData.add(map);
		map = new TpLanguage("IN", R.string.language_indonesian);
		mData.add(map);
		map = new TpLanguage("MS", R.string.language_malay);
		mData.add(map);
		map = new TpLanguage("NL", R.string.language_dutch);
		mData.add(map);
		map = new TpLanguage("AR", R.string.language_arabic);
		mData.add(map);
		map = new TpLanguage("FA", R.string.language_persian);
		mData.add(map);
		map = new TpLanguage("HI", R.string.language_hindi);
		//map=new TpLanguage("UD", R.string.language_urdu);
		mData.add(map);
		map = new TpLanguage("BN", R.string.language_bengali);
		mData.add(map);
	}

	private void initRadioGroup(){
		radioGroup = (RadioGroup)findViewById(R.id.lan_radioGroup);
		int index = 0;
		for(TpLanguage map:mData){
			RadioButton button = new RadioButton(this);
			button.setTextColor(getResources().getColor(R.color.black));
			button.setText(map.getStrId());
			button.setId(index);
			if(null != Globals.myInfo && map.getShortName().equals(Globals.myInfo.getLan()))
				button.setChecked(true);
			radioGroup.addView(button);
			index++;
		}
	}
	
	private void clickEvents() {
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SettingLanguageActivity.this.finish();
			}
		});
		
		btn_save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int index = radioGroup.getCheckedRadioButtonId();
				TPBuddyinfo buddyinfo = new TPBuddyinfo();
				if(mData.size() > 0){
					buddyinfo.setLan(mData.get(index).getShortName());
					tpChat.tp_chat_set_info(buddyinfo);
					if(null != Globals.myInfo){
						Globals.myInfo.setLan(mData.get(index).getShortName());
						Resources resources =getResources();
						Globals.myInfo.setLanstr(resources.getString(mData.get(index).getStrId()));
					}
					SettingLanguageActivity.this.finish();
				}
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mData = null;
	}
}
